package cn.middle.remote.util;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

public final class NettyAttributeUtil {
	
	private static String USER_NAME = "userName";
	
	private static String CLIENT_ID = "clientId";
	
	private static String IS_LOGIN = "is_login";
	
	public static String username(Channel channel) {
		return (String)channel.attr(AttributeKey.valueOf(USER_NAME)).get();
	}

	public static void username(Channel channel,String username) {
		channel.attr(AttributeKey.valueOf(USER_NAME)).set(username);
	}
	
	public static Integer clientId(Channel channel) {
		Attribute attribute = channel.attr(AttributeKey.valueOf(CLIENT_ID));
		if(attribute != null) {
			Object value = attribute.get();
			if(value != null) {
				return (Integer)value;
			}
		}
		return null;
	}
	
	public static void clientId(Channel channel,int clientId) {
		channel.attr(AttributeKey.valueOf(CLIENT_ID)).set(clientId);
	}
	
	public static boolean isLogin(Channel channel) {
		Object obj = channel.attr(AttributeKey.valueOf(IS_LOGIN)).get();
		if(obj == null) {
			return false;
		}
		return (boolean)obj;
	}
	
	public static void setLogin(Channel channel,boolean isLogin) {
		channel.attr(AttributeKey.valueOf(IS_LOGIN)).set(isLogin);
	}
	
	public static void set(Channel channel,String key,Object value) {
		channel.attr(AttributeKey.valueOf(key)).set(value);
	}
	
	public static Object get(Channel channel,String key) {
		return channel.attr(AttributeKey.valueOf(key)).get();
	}
}
